import time
import cv2

classifier = cv2.CascadeClassifier("./haarcascade_frontalface_alt.xml")

path = "./train/samkuang/"
n = 200

cap = cv2.VideoCapture(0)

i = 0
while True:
    ret, img = cap.read()
    img = cv2.resize(img, None, fx = 1, fy = 1)
    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    face_area = classifier.detectMultiScale(img_gray, 1.5, 5)
    for (x, y, w, h) in face_area:
        cv2.imwrite(path + str(i) + ".jpg", img_gray)
        i+=1
        print("Collecting face " + str(i))
    if i > n:
        break
    cv2.imshow("img", img)
    time.sleep(0.05)
    key = cv2.waitKey(1)
    if(key == ord('q')):
        print("Force cancel!")
        break
cap.release()
cv2.destroyAllWindows()

